import router from "./router";
// import store from './store'
import storage from "store";
import NProgress from "nprogress"; // progress bar
import "nprogress/nprogress.css"; // progress bar css
import { ACCESS_TOKEN } from "@/store/mutation-types";

NProgress.configure({ showSpinner: false }); // NProgress 设置

const whiteList = ["login", "register", "registerResult"]; // 重定向白名单

const loginRoutePath = "/user/login";
const defaultRoutePath = "/";

// 全局导航守卫
router.beforeEach((to, from, next) => {
  NProgress.start(); // 开始 progress bar
  if (storage.get(ACCESS_TOKEN)) {
    // 根据判断缓存是否有token判断账号是否需要登入
    if (to.path === loginRoutePath) {
      next({ path: defaultRoutePath });
      NProgress.done();
    } else {
      next();
    }
  } else {
    if (whiteList.includes(to.name)) {
      // 在免登录白名单，直接进入
      next();
    } else {
      next({ path: loginRoutePath, query: { redirect: to.fullPath } });
      NProgress.done();
    }
  }
});

router.afterEach(() => {
  NProgress.done(); // 结束 progress bar
});
